package com.thoughtworks.stafftimetable.parser;

import com.thoughtworks.stafftimetable.domain.Consultant;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class SourceDataParser {
    public List<Consultant> parse(String csvFile) throws ParseException {
        StringTokenizer tokenizer = new StringTokenizer(csvFile, "\n", true);
        // Skip the first header line
        tokenizer.nextToken();
        tokenizer.nextToken();

        List<Consultant> consultants = createConsultantsForEachLineInFile(tokenizer);
        return consultants;
    }

    private List<Consultant> createConsultantsForEachLineInFile(StringTokenizer tokenizer) throws ParseException {
        List<Consultant> consultants = new ArrayList<Consultant>();
        while (tokenizer.hasMoreTokens()) {
            String sourceLine = tokenizer.nextToken();

            // Not sure why there are empty lines being parsed here, but this is a hack to fix it.
            if (tokenizer.hasMoreTokens()) {
                String nullLine = tokenizer.nextToken();
            }

            SourceDataLineParser lineParser = new SourceDataLineParser();
            consultants = lineParser.parse(consultants, sourceLine);
        }

        return consultants;
    }
}
